package com.rz.htk.config;

import com.alibaba.nacos.api.config.annotation.NacosValue;
import com.xxl.job.core.executor.impl.XxlJobSpringExecutor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class XxlJobConfig {
    private Logger logger = LoggerFactory.getLogger("XxlJobConfig");

    @NacosValue(value = "${xxl.job.admin.addresses}", autoRefreshed = true)
    private String adminAddresses;
    @NacosValue(value = "${xxl.job.executor.appname}", autoRefreshed = true)
    private String appName;
    @NacosValue(value = "${xxl.job.executor.port}", autoRefreshed = true)
    private int port;
    @NacosValue(value = "${xxl.job.executor.logpath}", autoRefreshed = true)
    private String logpath;
    @NacosValue(value = "${xxl.job.executor.logretentiondays}", autoRefreshed = true)
    private int logRetentionDays;
    @NacosValue(value = "${enableScheduled}", autoRefreshed = true)
    private boolean enableScheduled;

    @Bean(initMethod = "start", destroyMethod = "destroy")
    public XxlJobSpringExecutor xxlJobExecutor() {
        if(enableScheduled){
            logger.info(">>>>>>>>>>> xxl-job config init.");
            XxlJobSpringExecutor xxlJobSpringExecutor = new XxlJobSpringExecutor();
            xxlJobSpringExecutor.setAdminAddresses(adminAddresses);
            xxlJobSpringExecutor.setAppName(appName);
            xxlJobSpringExecutor.setIp("");
            xxlJobSpringExecutor.setPort(port);
            xxlJobSpringExecutor.setAccessToken("");
            xxlJobSpringExecutor.setLogPath(logpath);
            xxlJobSpringExecutor.setLogRetentionDays(logRetentionDays);
            return xxlJobSpringExecutor;
        }
        return null;
    }
}
